package com.top.horizon.th1804app.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.net.protocol.model.CommonModel;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.base.BaseActivity;
import com.top.horizon.th1804app.constant.Constant;
import com.net.protocol.NetController;
import com.top.horizon.th1804app.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ChangePasswordActivity extends BaseActivity {

    private TextView tvAccounts;
    private EditText etNewPassword, etConfirmPassword;
    private String userName;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initView() {
        tvAccounts = findViewById(R.id.tv_accounts);
        etNewPassword = findViewById(R.id.et_new_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        findViewById(R.id.btn_confirm_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPassword = etNewPassword.getText().toString().trim();
                String confirmPassword = etConfirmPassword.getText().toString().trim();
                //验证密码是否符合规则，符合就发到服务器
                if (!TextUtils.isEmpty(newPassword)){
                    if (!TextUtils.isEmpty(confirmPassword)) {
                        if (newPassword.equals(confirmPassword)) {
                                if (newPassword.length() > 4 && newPassword.length() < 20){
                                    //发到服务器修改并保存
                                    showProgressDialog("加载中");
                                    NetController.modifyPwd(TextUtils.isEmpty(userName) ? "未知用户" : userName,newPassword);
                                }else {
                                    showToast("密码应为4到20之间的英文字符或数字！");
                                }

                        }
                    }else {
                        showToast("请确认新密码");
                    }
                }else {
                    showToast("新密码不能为空");
                }
            }
        });
    }

    @Override
    protected void initData() {
        userName = (String) SPUtils.get(this, Constant.USER_NAME, "");
        tvAccounts.setText(TextUtils.isEmpty(userName) ? "未知用户" : userName);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void commonEvent(CommonModel commonModel){
        hideProressDialog();
        if (commonModel.code == 0){
            showToast("操作成功");
            finish();
        }else {
            showToast("操作失败");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}
